package com.hyzx.qbasic.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.domain.service.IPermissionService;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.po.ResourceInfoPO;
import com.hyzx.qbasic.admin.model.po.ResourcePO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by 黄江华 on 2017/9/29
 */

@Api(tags = "资源管理", description = "ResourceController")
@Controller
@RequestMapping("/sys/resource")
public class ResourceController extends BaseController {

    @Autowired
    private IPermissionService permissionService;

    @SysLog("资源管理首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.SYS_RESOURCE_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.SYS_RESOURCE_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.SYS_RESOURCE_DELETE));
        return "system/resource/list";
    }

    @SysLog("获取资源列表")
    @GetMapping("/getResourceList")
    @ResponseBody
    public BaseResModel<PageInfo<ResourceInfoPO>> getResourceList(@Valid GetResourceListReq req, BindingResult result) {
        return BaseResUtils.success(permissionService.getPagedResourceList(req));
    }

    @SysLog("新增资源页")
    @GetMapping("/add")
    public String add() {
        return "system/resource/add";
    }

    @SysLog("新增资源表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddResourceReq req, BindingResult result) {
        BaseOperationRes res = permissionService.addResource(req);
        return BaseResUtils.success(res);
    }

    @SysLog("删除资源")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteResourceReq req, BindingResult result) {
        BaseOperationRes res = permissionService.deleteResource(req);
        return BaseResUtils.success(res);
    }

    @SysLog("编辑资源页")
    @GetMapping("/edit")
    public String edit() {
        return "system/resource/edit";
    }

    @SysLog("编辑资源表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditResourceReq req, BindingResult result) {
        BaseOperationRes res = permissionService.editResource(req);
        return BaseResUtils.success(res);
    }

    @SysLog("获取资源信息")
    @GetMapping("/getResourceInfo")
    @ResponseBody
    public BaseResModel<ResourcePO> getResourceInfo(@Valid GetResourceInfoReq req, BindingResult result) {
        return BaseResUtils.success(permissionService.getResourceInfo(req.getResourceId()));
    }
}
